﻿// Copyright (c) 2008 Interact Software Ltd.

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

// Dynamic Bitmap Generation for Silverlight
// =========================================
// See http://www.codeplex.com/SlDynamicBitmap for details.

using System;

namespace InteractSw.GenerateBitmap
{
    /// <summary>
    /// Conversion between numeric types and their network order (big endian) byte representations.
    /// </summary>
    class NetworkOrderBitConverter
    {
        /// <summary>
        /// Converts 4 bytes in network (big-endian) order into an UInt32.
        /// </summary>
        /// <param name="value">Array holding the bytes to be converted.</param>
        /// <param name="startIndex">Offset into array at which bytes are location.</param>
        /// <returns>The unsigned 32-bit value represented by the 4 bytes in the array.</returns>
        internal static uint ToUint32(byte[] value, int startIndex)
        {
            return (uint) ((value[startIndex] << 24) +
                           (value[startIndex + 1] << 16) +
                           (value[startIndex + 2] << 8) + 
                            value[startIndex + 3]);
        }


        /// <summary>
        /// Converts a UInt32 into its network order (big endian) byte representation.
        /// </summary>
        /// <param name="value">The 32-bit unsigned integer value to be converted.</param>
        /// <returns>A 4-byte array containing the big-endian representation of the value.</returns>
        internal static void GetBytes(uint value, byte[] buff, int offset)
        {
            buff[offset] = (byte) ((value >> 24) & 0xff);
            buff[offset + 1] = (byte) ((value >> 16) & 0xff);
            buff[offset + 2] = (byte) ((value >> 8) & 0xff);
            buff[offset + 3] = (byte) (value & 0xff);
        }

        /// <summary>
        /// Converts a UInt32 into its network order (big endian) byte representation.
        /// </summary>
        /// <param name="value">The 32-bit unsigned integer value to be converted.</param>
        /// <returns>A 4-byte array containing the big-endian representation of the value.</returns>
        internal static byte[] GetBytes(uint value)
        {
            byte[] bytes = new byte[4];
            GetBytes(value, bytes, 0);
            return bytes;
        }
    }
}
